Hanye官网
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

[id].vue 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. <template>
  2. <div>
  3. <div class="w-full h-[55px] sm:h-[72px]"></div>
  4. <ErrorBoundary :error="error">
  5. <div v-if="isLoading" class="flex justify-center py-12">
  6. <!-- 加载中 -->
  7. <div
  8. class="animate-spin h-8 w-8 border-4 border-cyan-400 rounded-full border-t-transparent"
  9. ></div>
  10. </div>
  11. <div v-else>
  12. <!-- 面包屑导航 -->
  13. <div class="max-w-full mb-6 xl:px-2 lg:px-2 md:px-4 px-4 mt-6">
  14. <div class="max-w-screen-2xl mx-auto">
  15. <nuxt-link
  16. :to="`${homepagePath}/`"
  17. class="justify-start text-white/60 text-base font-normal"
  18. >{{ t("common.breadcrumb.home") }}</nuxt-link
  19. >
  20. <span class="text-white/60 text-base font-normal px-2"> / </span>
  21. <nuxt-link
  22. v-if="product?.category"
  23. :to="`${homepagePath}/products?audiences=${product.meta?.audiences}`"
  24. class="text-white/60 text-base font-normal"
  25. >{{
  26. product.meta?.audiences === 0
  27. ? t("common.personal")
  28. : t("common.business")
  29. }}</nuxt-link
  30. >
  31. <span class="text-white/60 text-base font-normal px-2"> / </span>
  32. <nuxt-link
  33. v-if="product?.category"
  34. :to="`${homepagePath}/products?category=${encodeURIComponent(
  35. product.category
  36. )}&audiences=${product.meta?.audiences}`"
  37. class="text-white/60 text-base font-normal"
  38. >{{ product.category }}</nuxt-link
  39. >
  40. <span class="text-white/60 text-base font-normal px-2"> / </span>
  41. <span class="text-white text-base font-normal">{{
  42. product?.title || product?.name
  43. }}</span>
  44. </div>
  45. </div>
  46. <!-- 产品详情内容 -->
  47. <div
  48. v-if="product"
  49. class="max-w-full mb-12 md:mb-20 lg:mb-32 xl:px-2 lg:px-2 md:px-4 px-4"
  50. >
  51. <div class="max-w-screen-2xl mx-auto">
  52. <div class="grid grid-cols-1 lg:grid-cols-2 gap-8 lg:gap-16">
  53. <!-- 左侧产品图片 -->
  54. <div
  55. class="flex flex-col gap-6 lg:sticky lg:top-24 self-start select-none"
  56. >
  57. <!-- 主图展示 -->
  58. <div
  59. class="bg-zinc-900 rounded-lg p-8 relative overflow-hidden group aspect-square"
  60. >
  61. <!-- 主图容器 - Swiper -->
  62. <Swiper
  63. :modules="[Navigation, Pagination, EffectFade, Thumbs]"
  64. :slides-per-view="1"
  65. :pagination="{
  66. clickable: true,
  67. dynamicBullets: true,
  68. dynamicMainBullets: 3,
  69. }"
  70. :navigation="true"
  71. :effect="'fade'"
  72. :fade-effect="{ crossFade: true }"
  73. :thumbs="{ swiper: thumbsSwiper }"
  74. class="w-full h-full product-swiper"
  75. @swiper="swiperInstance = $event"
  76. @slideChange="currentSlideIndex = $event.activeIndex"
  77. >
  78. <SwiperSlide
  79. v-for="(image, slideIndex) in [
  80. product.image,
  81. ...(product.gallery || []),
  82. ]"
  83. :key="slideIndex"
  84. >
  85. <div class="relative w-full h-full">
  86. <!-- 当前图片 -->
  87. <img
  88. :src="image"
  89. :alt="`${product.name} - ${t('products.image', {
  90. index: slideIndex + 1,
  91. })}`"
  92. class="w-full h-full object-contain rounded-lg transition-all duration-500"
  93. :class="{
  94. 'opacity-0': isSlideThumbnailLoading[slideIndex],
  95. 'opacity-100':
  96. !isSlideThumbnailLoading[slideIndex] &&
  97. !slideThumbnailErrors[slideIndex],
  98. }"
  99. @load="handleSlideImageLoad(slideIndex)"
  100. @error="handleSlideImageError(slideIndex)"
  101. />
  102. <!-- 加载状态 -->
  103. <div
  104. v-if="isSlideThumbnailLoading[slideIndex]"
  105. class="absolute inset-0 flex items-center justify-center bg-zinc-800/50 z-10"
  106. >
  107. <div
  108. class="animate-spin h-8 w-8 border-4 border-cyan-400 rounded-full border-t-transparent"
  109. ></div>
  110. </div>
  111. <!-- 错误提示 -->
  112. <div
  113. v-if="slideThumbnailErrors[slideIndex]"
  114. class="absolute inset-0 flex items-center justify-center bg-red-900/50 z-20"
  115. >
  116. <div class="flex flex-col items-center gap-2">
  117. <span class="text-white">{{
  118. t("products.imageLoadError")
  119. }}</span>
  120. <button
  121. @click.stop="retryLoadSlideImage(slideIndex)"
  122. class="px-4 py-2 bg-cyan-400 text-white rounded-lg hover:bg-cyan-600 transition-colors duration-300"
  123. >
  124. {{ t("products.retry") }}
  125. </button>
  126. </div>
  127. </div>
  128. </div>
  129. </SwiperSlide>
  130. </Swiper>
  131. <!-- 全局加载状态 - 仅在所有图片都未加载完成时显示 -->
  132. <div
  133. v-if="isInitialLoading"
  134. class="absolute inset-0 flex items-center justify-center bg-zinc-900/80 z-30"
  135. >
  136. <div
  137. class="animate-spin h-12 w-12 border-4 border-cyan-400 rounded-full border-t-transparent"
  138. ></div>
  139. </div>
  140. </div>
  141. <!-- 缩略图列表 - Swiper -->
  142. <div
  143. class="relative bg-zinc-900 rounded-lg py-6 px-8 overflow-hidden product-thumbnail-container"
  144. >
  145. <Swiper
  146. :modules="[Navigation, Thumbs, FreeMode]"
  147. :slides-per-view="'auto'"
  148. :space-between="12"
  149. :free-mode="true"
  150. :watch-slides-progress="true"
  151. :navigation="{
  152. nextEl: '.swiper-thumb-next',
  153. prevEl: '.swiper-thumb-prev',
  154. }"
  155. class="thumbs-swiper"
  156. @swiper="thumbsSwiper = $event"
  157. >
  158. <SwiperSlide
  159. v-for="(image, index) in [
  160. product.image,
  161. ...(product.gallery || []),
  162. ]"
  163. :key="index"
  164. class="!w-16 !h-16 md:!w-20 md:!h-20 !flex-shrink-0 cursor-pointer transition-all duration-300 relative"
  165. >
  166. <div
  167. class="w-full h-full rounded-lg overflow-hidden thumbnail-fixed-size"
  168. :class="{
  169. 'ring-2 ring-cyan-400 ring-offset-2 ring-offset-zinc-900':
  170. currentSlideIndex === index,
  171. 'hover:ring-1 hover:ring-cyan-400/50 hover:ring-offset-1 hover:ring-offset-zinc-900':
  172. currentSlideIndex !== index,
  173. 'opacity-50':
  174. isSlideThumbnailLoading[index] ||
  175. slideThumbnailErrors[index],
  176. }"
  177. >
  178. <!-- 缩略图加载状态 -->
  179. <div
  180. v-if="isSlideThumbnailLoading[index]"
  181. class="absolute inset-0 flex items-center justify-center bg-zinc-800/50 rounded-lg z-10"
  182. >
  183. <div
  184. class="animate-spin h-4 w-4 border-2 border-cyan-400 rounded-full border-t-transparent"
  185. ></div>
  186. </div>
  187. <!-- 缩略图遮罩 -->
  188. <div
  189. class="absolute inset-0 transition-all duration-300 rounded-lg"
  190. :class="{
  191. 'bg-black/30': currentSlideIndex === index,
  192. 'bg-black/0 hover:bg-black/20':
  193. currentSlideIndex !== index,
  194. }"
  195. ></div>
  196. <img
  197. :src="image"
  198. :alt="`${product.name} - ${t('products.image', {
  199. index: index + 1,
  200. })}`"
  201. class="w-full h-full object-cover transition-all duration-300 rounded-lg"
  202. :class="{
  203. 'opacity-0': isSlideThumbnailLoading[index],
  204. 'opacity-100':
  205. !isSlideThumbnailLoading[index] &&
  206. !slideThumbnailErrors[index],
  207. 'hover:scale-110': currentSlideIndex !== index,
  208. }"
  209. @load="handleSlideImageLoad(index)"
  210. @error="handleSlideImageError(index)"
  211. />
  212. <!-- 缩略图错误提示 -->
  213. <div
  214. v-if="slideThumbnailErrors[index]"
  215. class="absolute inset-0 flex items-center justify-center bg-red-900/50 rounded-lg"
  216. >
  217. <div class="flex flex-col items-center gap-1">
  218. <span class="text-white text-xs">{{
  219. t("products.error")
  220. }}</span>
  221. <button
  222. @click.stop="retryLoadSlideImage(index)"
  223. class="px-2 py-1 bg-cyan-400 text-white text-xs rounded hover:bg-cyan-600 transition-colors duration-300"
  224. >
  225. {{ t("products.retry") }}
  226. </button>
  227. </div>
  228. </div>
  229. </div>
  230. </SwiperSlide>
  231. </Swiper>
  232. <!-- 缩略图导航按钮 -->
  233. <button
  234. class="swiper-thumb-prev absolute top-1/2 left-2 z-10 w-8 h-8 flex items-center justify-center bg-black/50 hover:bg-cyan-400 rounded-full transform -translate-y-1/2 transition-all duration-300"
  235. >
  236. <svg
  237. xmlns="http://www.w3.org/2000/svg"
  238. class="h-5 w-5 text-white"
  239. fill="none"
  240. viewBox="0 0 24 24"
  241. stroke="currentColor"
  242. >
  243. <path
  244. stroke-linecap="round"
  245. stroke-linejoin="round"
  246. stroke-width="2"
  247. d="M15 19l-7-7 7-7"
  248. />
  249. </svg>
  250. </button>
  251. <button
  252. class="swiper-thumb-next absolute top-1/2 right-2 z-10 w-8 h-8 flex items-center justify-center bg-black/50 hover:bg-cyan-400 rounded-full transform -translate-y-1/2 transition-all duration-300"
  253. >
  254. <svg
  255. xmlns="http://www.w3.org/2000/svg"
  256. class="h-5 w-5 text-white"
  257. fill="none"
  258. viewBox="0 0 24 24"
  259. stroke="currentColor"
  260. >
  261. <path
  262. stroke-linecap="round"
  263. stroke-linejoin="round"
  264. stroke-width="2"
  265. d="M9 5l7 7-7 7"
  266. />
  267. </svg>
  268. </button>
  269. </div>
  270. </div>
  271. <!-- 右侧产品信息 -->
  272. <div class="flex flex-col gap-8">
  273. <!-- 产品名称 -->
  274. <div class="bg-zinc-900 rounded-lg p-6">
  275. <h1 class="text-white text-3xl font-medium mb-4">
  276. {{ product.title || product.name }}
  277. </h1>
  278. <div class="text-[#71717A] text-lg leading-relaxed">
  279. {{ product.summary }}
  280. </div>
  281. </div>
  282. <!-- 产品参数 -->
  283. <div class="bg-zinc-900 rounded-lg p-6">
  284. <div class="grid grid-cols-1 gap-4">
  285. <div
  286. class="flex justify-between items-center py-2 border-b border-zinc-800"
  287. >
  288. <span class="text-[#71717A]">{{
  289. t("products.categoryTitle")
  290. }}</span>
  291. <span class="text-white font-medium"
  292. >{{ product.category
  293. }}<template v-if="product.tag">
  294. / {{ product.tag }}</template
  295. ></span
  296. >
  297. </div>
  298. <div
  299. v-if="product.tag"
  300. class="flex justify-between items-center py-2 border-b border-zinc-800"
  301. >
  302. <span class="text-[#71717A]">{{
  303. t("products.seriesTitle")
  304. }}</span>
  305. <span class="text-white font-medium">{{
  306. product.series?.join(" / ")
  307. }}</span>
  308. </div>
  309. <div
  310. class="flex justify-between items-center py-2 border-b border-zinc-800"
  311. >
  312. <span class="text-[#71717A]">{{
  313. t("products.usageTitle")
  314. }}</span>
  315. <span class="text-white font-medium">{{
  316. product.usage?.join(", ")
  317. }}</span>
  318. </div>
  319. <div
  320. v-if="product.capacities && product.capacities.length > 0"
  321. class="flex justify-between items-center py-2"
  322. >
  323. <span class="text-[#71717A]">{{
  324. t("products.capacitiesTitle")
  325. }}</span>
  326. <span class="text-white font-medium">{{
  327. product.capacities?.join(" / ")
  328. }}</span>
  329. </div>
  330. </div>
  331. </div>
  332. <!-- 产品描述 -->
  333. <div
  334. v-if="product.description"
  335. class="bg-zinc-900 rounded-lg p-6"
  336. >
  337. <h2 class="text-white text-xl font-medium mb-6">
  338. {{ t("products.productDescription") }}
  339. </h2>
  340. <div
  341. class="text-[#71717A] leading-relaxed space-y-4 prose prose-invert max-w-none"
  342. >
  343. {{ product.description }}
  344. </div>
  345. </div>
  346. <!-- 个人用户产品描述 -->
  347. <div
  348. v-if="product.meta?.audiences === 0"
  349. class="bg-zinc-900 rounded-lg p-6"
  350. >
  351. <div
  352. class="text-[#71717A] leading-relaxed space-y-4 prose prose-invert max-w-none"
  353. >
  354. <ContentRenderer :value="product.content" />
  355. </div>
  356. </div>
  357. <!-- 相关产品 -->
  358. <div
  359. v-if="relatedProducts.length > 0"
  360. class="bg-zinc-900 rounded-lg p-6"
  361. >
  362. <h2 class="text-white text-xl font-medium mb-6">
  363. {{ t("products.relatedProducts") }}
  364. </h2>
  365. <div
  366. class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4"
  367. >
  368. <nuxt-link
  369. v-for="relatedProduct in relatedProducts"
  370. :key="relatedProduct.id"
  371. :to="`${homepagePath}/products/${relatedProduct.id}`"
  372. class="group"
  373. >
  374. <div
  375. class="bg-zinc-800 rounded-lg p-4 transition-all duration-300 hover:bg-zinc-700"
  376. >
  377. <div
  378. class="aspect-square mb-4 overflow-hidden rounded-lg"
  379. >
  380. <img
  381. :src="relatedProduct.image"
  382. :alt="relatedProduct.title || relatedProduct.name"
  383. class="w-full h-full object-cover transition-transform duration-300 group-hover:scale-110"
  384. />
  385. </div>
  386. <h3
  387. class="text-white text-lg font-medium mb-2 line-clamp-2"
  388. >
  389. {{ relatedProduct.title || relatedProduct.name }}
  390. </h3>
  391. <p class="text-[#71717A] text-sm line-clamp-2">
  392. {{ relatedProduct.summary }}
  393. </p>
  394. </div>
  395. </nuxt-link>
  396. </div>
  397. </div>
  398. </div>
  399. </div>
  400. <!-- 企业用户产品描述 -->
  401. <div
  402. v-if="product.meta?.audiences === 1"
  403. class="bg-zinc-900 rounded-lg p-6 mt-6"
  404. >
  405. <div
  406. class="text-[#71717A] leading-relaxed space-y-4 prose prose-invert max-w-none"
  407. >
  408. <ContentRenderer :value="product.content" />
  409. </div>
  410. </div>
  411. </div>
  412. </div>
  413. </div>
  414. </ErrorBoundary>
  415. </div>
  416. </template>
  417. <script setup lang="ts">
  418. /**
  419. * 产品详情页面
  420. * 展示产品主图、参数和描述
  421. */
  422. import { useErrorHandler } from "~/composables/useErrorHandler";
  423. import { useRoute, useI18n, useAsyncData } from "#imports";
  424. import { queryCollection } from "#imports";
  425. import { ContentRenderer } from "#components";
  426. import { Swiper, SwiperSlide } from "swiper/vue";
  427. import {
  428. Navigation,
  429. Pagination,
  430. EffectFade,
  431. Thumbs,
  432. FreeMode,
  433. } from "swiper/modules";
  434. import type { Swiper as SwiperType } from "swiper";
  435. import "swiper/css";
  436. import "swiper/css/navigation";
  437. import "swiper/css/pagination";
  438. import "swiper/css/effect-fade";
  439. const { error, isLoading } = useErrorHandler();
  440. const route = useRoute();
  441. const { locale, t } = useI18n();
  442. const id = route.params.id as string;
  443. const swiperInstance = ref<SwiperType | null>(null);
  444. const thumbsSwiper = ref<SwiperType | null>(null);
  445. // 图片状态
  446. const currentSlideIndex = ref(0);
  447. const isInitialLoading = ref(true); // 初始加载状态
  448. const isSlideThumbnailLoading = ref<boolean[]>([]);
  449. const slideThumbnailErrors = ref<boolean[]>([]);
  450. // 滚动跟随相关
  451. const scrollContainer = ref<HTMLElement | null>(null);
  452. const isSticky = ref(false);
  453. const homepagePath = computed(() => {
  454. return locale.value === "zh" ? "" : `/${locale.value}`;
  455. });
  456. interface Product {
  457. id: string;
  458. name: string;
  459. usage: string[];
  460. capacities: string[];
  461. category: string;
  462. categoryId: string;
  463. description: string;
  464. summary: string;
  465. image: string;
  466. gallery: string[];
  467. body: string;
  468. content?: any;
  469. tag?: string;
  470. series?: string[];
  471. meta?: {
  472. series?: string[];
  473. name?: string;
  474. title?: string;
  475. image?: string;
  476. summary?: string;
  477. audiences: number;
  478. };
  479. title?: string;
  480. }
  481. /**
  482. * 使用queryCollection获取产品数据
  483. */
  484. const { data: productContent } = await useAsyncData(
  485. `product-${id}`,
  486. async () => {
  487. try {
  488. // 使用queryCollection从content目录获取数据
  489. const content = await queryCollection("content")
  490. .where("path", "LIKE", `/products/${locale.value}/${id}`)
  491. .first();
  492. return content;
  493. } catch (err) {
  494. console.error("Error fetching product content:", err);
  495. error.value = new Error(t("products.loadError"));
  496. return null;
  497. }
  498. }
  499. );
  500. /**
  501. * 获取分类信息
  502. */
  503. const { data: categoryContent } = await useAsyncData(
  504. `category-${productContent.value?.meta?.categoryId}`,
  505. async () => {
  506. if (!productContent.value?.meta?.categoryId) return null;
  507. try {
  508. const content = await queryCollection("content")
  509. .where(
  510. "path",
  511. "LIKE",
  512. `/categories/${locale.value}/${productContent.value.meta?.categoryId}`
  513. )
  514. .first();
  515. return content;
  516. } catch (err) {
  517. console.error("Error fetching category:", err);
  518. return null;
  519. }
  520. },
  521. {
  522. immediate: !!productContent.value?.meta?.categoryId,
  523. }
  524. );
  525. /**
  526. * 使用计算属性解析产品数据
  527. */
  528. const product = computed<Product | any>(() => {
  529. if (!productContent.value) return null;
  530. // 提取产品数据
  531. const meta = productContent.value.meta || {};
  532. return {
  533. id: id,
  534. name: String(meta.name || productContent.value.title || ""),
  535. title: String(productContent.value.title || meta.name || ""),
  536. usage: Array.isArray(meta.usage) ? meta.usage : [],
  537. capacities: Array.isArray(meta.capacities) ? meta.capacities : [],
  538. category: categoryContent.value?.title || "",
  539. categoryId: meta.categoryId || "",
  540. description: productContent.value.description || "",
  541. summary: String(meta.summary || ""),
  542. image: String(meta.image || ""),
  543. gallery: Array.isArray(meta.gallery) ? meta.gallery : [],
  544. body: productContent.value.body || "",
  545. content: productContent.value,
  546. tag: meta.tag || "",
  547. series: Array.isArray(meta.series) ? meta.series : [],
  548. meta: {
  549. series: Array.isArray(meta.series) ? meta.series : [],
  550. name: String(meta.name || ""),
  551. title: String(productContent.value.title || ""),
  552. image: String(meta.image || ""),
  553. summary: String(meta.summary || ""),
  554. audiences: categoryContent.value?.meta?.audiences || 0,
  555. },
  556. };
  557. });
  558. /**
  559. * 获取相关产品
  560. */
  561. const { data: relatedProductsContent } = await useAsyncData(
  562. `related-products-${id}`,
  563. async () => {
  564. try {
  565. // 获取产品列表
  566. const content = await queryCollection("content")
  567. .where("path", "LIKE", `/products/${locale.value}/%`)
  568. .all();
  569. return content;
  570. } catch (err) {
  571. console.error("Error fetching related products:", err);
  572. return [];
  573. }
  574. }
  575. );
  576. /**
  577. * 处理相关产品数据
  578. */
  579. const relatedProducts = computed(() => {
  580. if (!relatedProductsContent.value || !product.value) return [];
  581. // 获取当前产品的分类和系列
  582. const currentCategory = product.value.categoryId;
  583. const currentSeries = product.value.meta?.series || [];
  584. const currentProductId = id;
  585. return relatedProductsContent.value
  586. .filter((item: any) => {
  587. // 排除当前产品 - 多重检查确保排除
  588. if (item._path === `/products/${locale.value}/${id}`) return false;
  589. const meta = item.meta || {};
  590. if (meta.name === currentProductId) return false;
  591. const itemCategoryId = meta.categoryId || "";
  592. const itemSeries = Array.isArray(meta.series) ? meta.series : [];
  593. // 判断是否同类别或同系列
  594. const isSameCategory =
  595. currentCategory && itemCategoryId === currentCategory;
  596. const hasSameSeries =
  597. currentSeries.length > 0 &&
  598. itemSeries.some((series: string) => currentSeries.includes(series));
  599. // 返回同类别或同系列的产品
  600. return isSameCategory || hasSameSeries;
  601. })
  602. .map((item: any) => {
  603. const meta = item.meta || {};
  604. return {
  605. id: meta.name || "",
  606. name: meta.name || item.title || "",
  607. title: item.title || meta.name || "",
  608. image: meta.image || "",
  609. summary: meta.summary || "",
  610. category: meta.categoryId || "",
  611. series: Array.isArray(meta.series) ? meta.series : [],
  612. };
  613. })
  614. .slice(0, 6); // 最多显示6个相关产品
  615. });
  616. /**
  617. * 预加载下一张图片
  618. */
  619. function preloadNextImage(image: string) {
  620. currentSlideIndex.value =
  621. (currentSlideIndex.value + 1) % (product.value?.gallery?.length || 1);
  622. }
  623. /**
  624. * 处理图片加载完成
  625. */
  626. function handleSlideImageLoad(index: number) {
  627. isSlideThumbnailLoading.value[index] = false;
  628. slideThumbnailErrors.value[index] = false;
  629. // 检查是否所有图片都已加载
  630. const allImagesLoaded = isSlideThumbnailLoading.value.every(
  631. (status) => !status
  632. );
  633. if (allImagesLoaded) {
  634. isInitialLoading.value = false;
  635. }
  636. }
  637. /**
  638. * 处理图片加载错误
  639. */
  640. function handleSlideImageError(index: number) {
  641. isSlideThumbnailLoading.value[index] = false;
  642. slideThumbnailErrors.value[index] = true;
  643. }
  644. /**
  645. * 重试加载图片
  646. */
  647. function retryLoadSlideImage(index: number) {
  648. isSlideThumbnailLoading.value[index] = true;
  649. slideThumbnailErrors.value[index] = false;
  650. // 确定正确的图片URL
  651. const images = [product.value?.image, ...(product.value?.gallery || [])];
  652. const imageUrl = images[index];
  653. // 检查图片URL是否有效
  654. if (!imageUrl) {
  655. console.error("Invalid image URL:", index);
  656. slideThumbnailErrors.value[index] = true;
  657. isSlideThumbnailLoading.value[index] = false;
  658. return;
  659. }
  660. // 创建新的图片对象并设置超时
  661. const img = new Image();
  662. const timeoutId = setTimeout(() => {
  663. handleSlideImageError(index);
  664. }, 10000); // 10秒超时
  665. img.onload = () => {
  666. clearTimeout(timeoutId);
  667. handleSlideImageLoad(index);
  668. };
  669. img.onerror = (error) => {
  670. clearTimeout(timeoutId);
  671. console.error("Image load error:", { index, error });
  672. handleSlideImageError(index);
  673. };
  674. // 设置跨域属性
  675. img.crossOrigin = "anonymous";
  676. // 最后设置src以开始加载
  677. img.src = imageUrl;
  678. }
  679. // 页面加载时初始化状态
  680. onMounted(() => {
  681. // 初始化缩略图加载状态数组
  682. const galleryLength = (product.value?.gallery?.length || 0) + 1;
  683. isSlideThumbnailLoading.value = new Array(galleryLength).fill(true);
  684. slideThumbnailErrors.value = new Array(galleryLength).fill(false);
  685. // 设置初始加载状态
  686. isInitialLoading.value = true;
  687. // 预加载所有缩略图
  688. const images = [product.value?.image, ...(product.value?.gallery || [])];
  689. images.forEach((image, index) => {
  690. if (image) {
  691. const img = new Image();
  692. img.onload = () => handleSlideImageLoad(index);
  693. img.onerror = () => handleSlideImageError(index);
  694. img.src = image;
  695. }
  696. });
  697. // 添加滚动监听
  698. scrollContainer.value = document.querySelector(".max-w-screen-2xl");
  699. if (scrollContainer.value) {
  700. window.addEventListener("scroll", handleScroll, { passive: true });
  701. }
  702. });
  703. // 清理滚动监听
  704. onUnmounted(() => {
  705. if (scrollContainer.value) {
  706. window.removeEventListener("scroll", handleScroll);
  707. }
  708. });
  709. // 处理滚动事件
  710. function handleScroll() {
  711. if (!scrollContainer.value) return;
  712. const containerRect = scrollContainer.value.getBoundingClientRect();
  713. const scrollTop = window.scrollY || document.documentElement.scrollTop;
  714. // 当容器顶部距离视窗顶部小于100px时,启用sticky
  715. isSticky.value = containerRect.top < 100;
  716. }
  717. // SEO优化
  718. useHead(() => ({
  719. title: `${product.value?.name} - Hanye`,
  720. meta: [
  721. {
  722. name: "description",
  723. content: product.value?.description,
  724. },
  725. ],
  726. }));
  727. </script>
  728. <style lang="scss" scoped>
  729. /* 隐藏滚动条但保持滚动功能 */
  730. .scrollbar-hide {
  731. -ms-overflow-style: none; /* IE and Edge */
  732. scrollbar-width: none; /* Firefox */
  733. }
  734. .scrollbar-hide::-webkit-scrollbar {
  735. display: none; /* Chrome, Safari and Opera */
  736. }
  737. /* 图片过渡动画 */
  738. .main-image {
  739. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  740. }
  741. /* 缩略图悬停效果 */
  742. .thumbnail-item {
  743. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  744. }
  745. .thumbnail-item:hover {
  746. transform: translateY(-2px);
  747. }
  748. /* 缩略图选中效果 */
  749. .thumbnail-item.selected {
  750. transform: scale(1.05);
  751. box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1),
  752. 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  753. }
  754. /* 产品信息卡片效果 */
  755. .info-card {
  756. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  757. }
  758. .info-card:hover {
  759. transform: translateY(-2px);
  760. box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1),
  761. 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  762. }
  763. /* 滚动跟随效果 */
  764. .lg\:sticky {
  765. position: sticky;
  766. top: 6rem; /* 96px */
  767. transition: all 0.3s ease;
  768. z-index: 10;
  769. max-height: calc(100vh - 6rem);
  770. overflow-y: auto;
  771. }
  772. @media (max-width: 1024px) {
  773. .lg\:sticky {
  774. position: relative;
  775. top: 0;
  776. max-height: none;
  777. }
  778. }
  779. /* Swiper 样式定制 */
  780. .product-swiper {
  781. :deep(.swiper-pagination-bullet) {
  782. background-color: white;
  783. opacity: 0.5;
  784. }
  785. :deep(.swiper-pagination-bullet-active) {
  786. opacity: 1;
  787. background-color: theme("colors.cyan.400");
  788. }
  789. :deep(.swiper-button-next),
  790. :deep(.swiper-button-prev) {
  791. color: theme("colors.cyan.500");
  792. background-color: rgba(0, 0, 0, 0.3);
  793. width: 36px;
  794. height: 36px;
  795. border-radius: 50%;
  796. &:after {
  797. font-size: 16px;
  798. font-weight: bold;
  799. }
  800. &:hover {
  801. background-color: theme("colors.cyan.500");
  802. color: white;
  803. }
  804. }
  805. :deep(.swiper-button-disabled) {
  806. opacity: 0.35;
  807. cursor: auto;
  808. pointer-events: none;
  809. }
  810. :deep(.swiper-slide) {
  811. display: flex;
  812. align-items: center;
  813. justify-content: center;
  814. }
  815. }
  816. /* 缩略图Swiper样式 */
  817. .thumbs-swiper {
  818. overflow: visible;
  819. padding: 0.25rem;
  820. min-height: 6rem;
  821. :deep(.swiper-wrapper) {
  822. align-items: center;
  823. display: flex;
  824. min-height: 6rem;
  825. }
  826. :deep(.swiper-slide) {
  827. width: auto;
  828. height: auto;
  829. opacity: 0.7;
  830. transition: all 0.3s ease;
  831. display: flex;
  832. align-items: center;
  833. justify-content: center;
  834. }
  835. :deep(.swiper-slide-thumb-active) {
  836. opacity: 1;
  837. transform: scale(1.05);
  838. z-index: 1;
  839. }
  840. }
  841. /* 自定义缩略图导航按钮 */
  842. .swiper-thumb-next,
  843. .swiper-thumb-prev {
  844. &:focus {
  845. outline: none;
  846. }
  847. &.swiper-button-disabled {
  848. opacity: 0.3;
  849. cursor: default;
  850. background-color: rgba(0, 0, 0, 0.2);
  851. &:hover {
  852. background-color: rgba(0, 0, 0, 0.2);
  853. }
  854. }
  855. }
  856. /* 响应式调整 */
  857. @media (max-width: 768px) {
  858. .thumbs-swiper {
  859. padding: 0;
  860. :deep(.swiper-slide) {
  861. margin-right: 8px;
  862. }
  863. }
  864. /* 确保缩略图容器在移动设备上有足够空间 */
  865. .product-thumbnail-container {
  866. padding: 1rem;
  867. .swiper-thumb-prev {
  868. left: 0.5rem;
  869. width: 2rem;
  870. height: 2rem;
  871. }
  872. .swiper-thumb-next {
  873. right: 0.5rem;
  874. width: 2rem;
  875. height: 2rem;
  876. }
  877. }
  878. }
  879. /* 解决不同分辨率下缩略图大小问题 */
  880. .thumbnail-fixed-size {
  881. width: 100%;
  882. height: 100%;
  883. aspect-ratio: 1 / 1;
  884. position: relative;
  885. }
  886. </style>